﻿using ConsoleApp7;
using IRealize;
using Microsoft.AspNetCore.Mvc;
using Realize;
using Entity;
using Microsoft.Extensions.Configuration;
using ConsoleApp7.Filter;
using Hangfire;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace WebApplication9.Controllers
{
    public class TextController : Controller
    {
        private readonly ILogger<TextController> _logger;
        private readonly IConfiguration _configuration;
        private IEmailService _emailService;
        public TextController(ILogger<TextController> logger, IConfiguration configuration,IEmailService emailService)
        {
            _logger = logger;
            _configuration = configuration;
            _emailService = emailService;
        }
        public IActionResult Login()
        {
            //Log_file_storage.LogDebug(_logger, "第20行");
            //Log_file_storage.LogInformation(_logger, "第18行");
            //Log_file_storage.LogWarning(_logger, "第22行");
            //Log_file_storage.LogError(_logger, "第23行");
            //Log_file_storage.LogCritical(_logger, "第24行");
            //Log_file_storage.LogTrace(_logger, "第28行");
            ViewBag.s = "TextController.Login";
            return View();
        }
        public IActionResult Homepage() 
        {
            ViewBag.s = "TextController.Homepage";
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> Login(string userName,string pwd)
        {
            //Log_file_storage.LogDebug(_logger, "第20行");
            //Log_file_storage.LogInformation(_logger, "第18行");
            //Log_file_storage.LogWarning(_logger, "第22行");
            //Log_file_storage.LogError(_logger, "第23行");
            //Log_file_storage.LogCritical(_logger, "第24行");
            //Log_file_storage.LogTrace(_logger, "第28行");
            IUsers us = new Users();
            User zhi = us.Login(userName, pwd, _configuration["SqlServer"]);
            if (zhi != null)
            {
                List<UserRoles> rid = await us.Select2(zhi.u_id, _configuration["SqlServer"]);
                HttpContext.Session.SetString("RID", rid[0].RolesID.ToString());
                //Console.WriteLine(rid[0].RolesID.ToString());
                HttpContext.Session.SetString("PName",zhi.u_true_name);
                HttpContext.Session.SetString("UPName",zhi.u_name);
                /*RecurringJob.AddOrUpdate("job名", () =>
                    _emailService.SendEmail("Recurring Job", DateTime.Now.ToLongTimeString()), Cron.Minutely);*/
                
                return RedirectToAction("Index", "Main");
            }
            return Content("登录失败");
        }
    }
}
